import 'dart:convert';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:mpc_plugin/mpc_plugin.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _mpcPlugin = MpcPlugin();
  Map<String, dynamic>? mpcList;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.

    var mpc = await _mpcPlugin.forgingMPC("12345678", "");
    mpcList = mpc;
    if (mpc != null) {
      try{
        print("mpc['mpcPieceA']:::::: ${mpc["mpcPieceA"].length}");
        print("mpc['mpcPieceB']:::::: ${mpc["mpcPieceB"]}");
        print("mpc['mpcPieceB']:::::: ${mpc["mpcPieceB"].length}");
        print("mpc['mpcPieceC']:::::: ${mpc["mpcPieceC"].length}");

        var versionNo = await _mpcPlugin.getVersionFromFrag(mpc["mpcPieceA"]);
        print('versionNo--::::::: $versionNo');

        final myPieceB= await _mpcPlugin.recoverMPCFragment(mpc["mpcPieceA"],mpc["mpcPieceC"]);
        print('recoverMPCFragment myPieceB ::::: ${myPieceB!['result']}');
        print('recoverMPCFragment myPieceB.length ::::: ${myPieceB['result'].length}');
        print('mpc["mpcPieceB"]:::::: ${mpc["mpcPieceB"].length}');
        print('${myPieceB['result']==mpc["mpcPieceB"]}');
        final tx = '01000103fb4d6416b46eecc1d9bfec516a06cc16880bdc31b0568687ecd0ef4c49142c5e4c0a90324315b1e012de41cc0f8c890e2ac593fa06327e32a3f958606d937adb0000000000000000000000000000000000000000000000000000000000000000d9f26a5a95617779432182900fe61b7c7c90e3bf342230a5ed72fd36fef8581f01020200010c0200000000e1f50500000000';
        var signInfo = await _mpcPlugin.solSignTx(tx,mpc["mpcPieceA"],mpc["mpcPieceB"],'12345678' );
        print('bscAddress::::::::::: $signInfo');
        // var ethAddress = await _mpcPlugin.getAddress(mpc["mpcPieceA"], "1");
        // var ethAddress = await _mpcPlugin.getAddress(mpc["mpcPieceA"], "1");
        // var ethAddress = await _mpcPlugin.getAddress(mpc["mpcPieceA"], "1");
        // var ethAddress = await _mpcPlugin.getAddress(mpc["mpcPieceA"], "1");
        // var ethAddress = await _mpcPlugin.getAddress(mpc["mpcPieceA"], "1");
      }catch(e){
        print('error :::::::::::: $e');
      }


      // var recover = await _mpcPlugin.recoverMPCFragment(
      //     useMpc["mpcPieceA"], useMpc["mpcPieceB"]);

      // print("recover:" + recover!["result"]);
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      // _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Container(
          padding: EdgeInsets.only(left: 20, right: 20),
          child: Column(
            children: [],
          ),
        ),
      ),
    );
  }
}
